package com.hckj.medical.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hckj.medical.service.IBookingService;
import com.hckj.medical.utils.Code;
import com.hckj.medical.utils.GetDate;
import com.hckj.medical.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import com.hckj.medical.entity.Booking;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hckj
 * @since 2023-11-25
 */
@RestController
@RequestMapping("/medical/booking")
public class BookingController {
    @Autowired
    private IBookingService bookingService;
    @PostMapping("/selectBydocId")
    public R selectBydid(@RequestBody Map map){
        List<Map> maps = bookingService.findAllBydocId(map);
        if (!maps.isEmpty()){
            return new R(Code.GET_OK,maps,"查询成功");
        }else {
            return new R(Code.GET_ERRO,"查询失败");
        }
    }

    //根据预约单id和就诊科室id查询预约单
    @GetMapping("/selectBookingByidAnddepartment/{id}/{department}")
    public R selectBookingByidAnddepartment(@PathVariable Integer id, @PathVariable Integer department){
        List<Booking> bookings = bookingService.selectBookingByidAnddepartment(id, department);
        if (!bookings.isEmpty()){
            return new R(Code.GET_OK,bookings,"查询成功");
        }else {
            return new R(Code.GET_ERRO,"查询失败");
        }
    }

    //添加预约单
    @PostMapping("/InsertBooking")
    public R InsertBooking(@RequestBody Booking booking){
        UUID uuid = UUID.randomUUID();
        UUID uuid1 = UUID.randomUUID();
        String s =  uuid.randomUUID().toString().substring(28).replaceAll("-","");
        String s1 =  uuid1.randomUUID().toString().substring(25).replaceAll("-","");
        booking.setBnumber(s);
        booking.setBnumbering(s1);
        if (booking.getId()==null){
            Integer integer = bookingService.InsertBooking(booking);
            if (integer>0){
                return new R(Code.SAVE_OK,booking.getId(),"添加成功");
            }else {
                return new R(Code.SAVE_ERRO,"添加失败");
            }
        }else {
            Integer integer = bookingService.updateBookingyy(booking);
            if (integer>0){
                return new R(Code.SAVE_OK,booking.getId(),"修改成功");
            }else {
                return new R(Code.SAVE_ERRO,"修改失败");
            }
        }


    }
    //根据id查询
    @GetMapping("/selectBookingById/{id}")
    public R selectBookingById(@PathVariable Integer id){
        Booking booking = bookingService.selectBookingById(id);
        if (booking!=null){
            return new R(Code.GET_OK,booking,"查询成功");
        }else {
            return new R(Code.GET_ERRO,"查询成功");
        }
    }
    //取消预约
    @PostMapping("/updatebstatus")
    public R updatebstatus(@RequestBody Booking booking){
        Integer updatebstatus = bookingService.updatebstatus(booking);
        if (updatebstatus>0){
            return new R(Code.UPDATE_OK,"修改成功");
        }else {
            return new R(Code.UPDATE_ERRO,"修改失败");
        }
    }

    //修改预约单
    @PostMapping("/updayeBooking")
    public R updayeBooking(@RequestBody Booking booking){
        Integer updayeBooking = bookingService.updateBooking(booking);
        if (updayeBooking>0){
            return new R(Code.UPDATE_OK,"修改成功");
        }else {
            return new R(Code.UPDATE_ERRO,"修改失败");
        }
    }

    //查询预约成功  预约失败的预约单
        @PostMapping("/selectBookingBybstatus/{userid}")
   public R  selectBookingBybstatus(@RequestBody List<Integer> bstatus, @PathVariable Integer userid){
        List<Booking> bookings = bookingService.selectBookingBybstatus(bstatus,userid);
        if (bookings!=null&&bookings.size()>0){
            return new R(Code.GET_OK,bookings,"查询成功");
        }else {
            return new R(Code.GET_ERRO,"查询失败");
        }
    }

    /**
     * 根据条件查询所有的预约
     * @param map
     * @return
     */
    @PostMapping("/selectBookingBymap")
    public R selectBooking(@RequestBody Map map){
        Page<Map> allByuser = bookingService.findAllByuser(map);
        if (allByuser != null){
            return new R(Code.GET_OK,allByuser,"查询成功");
        }else {
            return new R(Code.GET_ERRO,"查询失败");
        }
    }
//    根据id查询信息
    @GetMapping("/findbyid/{id}")
    public  R findByid(@PathVariable Integer id){
        Map byid = bookingService.findByid(id);
        if (byid.entrySet().size() > 0){
            return new R(Code.GET_OK,byid,"查询成功");
        }else {
            return new R(Code.GET_ERRO,"查询失败");
        }
    }

//    用户中心预约管理审核
    @PostMapping("/updatebyid")
    public R updatebyId(@RequestBody Booking booking){
        booking.setReviewtime(GetDate.getTime());
        bookingService.updateByid(booking);
        return new R(Code.UPDATE_OK,"修改成功");
    }


}
